package com.asa.common.widgets;

import android.animation.AnimatorSet;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;



/**
 * Author: halo_huang
 * Create: 2018/5/8
 * Describe:
 */

public class AnimClickTextView extends AppCompatTextView { //DayNightTextView
    private AnimatorSet mDownAnim;
    private AnimatorSet mUpAnim;
    private boolean mIsAnimEnable = true;

    public AnimClickTextView(Context context) {
        super(context);
        initAnim();
    }

    public AnimClickTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAnim();
    }

    public AnimClickTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAnim();
        setIncludeFontPadding(false);
    }

    public void setAnimEnable(boolean isAnimEnable){
        mIsAnimEnable = isAnimEnable;
    }

    private void initAnim() {
        mDownAnim = ButtonAnim.loadDownAnim(this);
        mUpAnim = ButtonAnim.loadUpAnim(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!mIsAnimEnable){
            return super.onTouchEvent(event);
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mDownAnim.start();
        } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
            mDownAnim.cancel();
            mUpAnim.start();
        }
        return super.onTouchEvent(event);
    }
}
